/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.bluepumpkin.beans;

import com.aptech.bluepumpkin.Registration;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Trong Nguyen
 */
@Stateless
public class RegistrationFacade extends AbstractFacade<Registration> {

    @PersistenceContext(unitName = "BluePumkin-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public RegistrationFacade() {
        super(Registration.class);
    }

    public List<Registration> getAvailableRegistration() {
        Calendar cal = Calendar.getInstance();

        // get start date

        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        Date startRegDate = cal.getTime();

        cal.add(Calendar.DAY_OF_MONTH, -1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        Date endRegDate = cal.getTime();
        String sql = "SELECT r FROM Registration r, Event e WHERE r.event=e AND e.endRegDate>=:endRegDate AND e.startRegDate<=:startRegDate";
        TypedQuery<Registration> query = em.createQuery(sql, Registration.class);
        query.setParameter("endRegDate", endRegDate);
        query.setParameter("startRegDate", startRegDate);
        List<Registration> resultList = query.getResultList();
        if (null != resultList) {
            System.out.println("getAvailableRegistration------found: " + resultList.size());
        }
        else{
            System.out.println("getAvailableRegistration------Not found-------------------");
        }
        return resultList;
    }
}
